package com.lian.hope.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lian.hope.mapper.DynamicMapper;
import com.lian.hope.mapper.UsersMapper;
import com.lian.hope.model.dto.DynamicCommentDTO;
import com.lian.hope.model.dto.DynamicDTO;
import com.lian.hope.model.po.Dynamic;
import com.lian.hope.model.po.DynamicComment;
import com.lian.hope.model.po.Users;
import com.lian.hope.model.vo.DynamicCommentVO;
import com.lian.hope.model.vo.DynamicVO;
import com.lian.hope.model.vo.PageBean;
import com.lian.hope.model.vo.UsersVO;
import com.lian.hope.service.DynamicService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DynamicServiceImpl implements DynamicService {

    private final DynamicMapper dynamicMapper;
    private final UsersMapper usersMapper;

    @Override
    public void insertDynamic(DynamicDTO dynamic, Long userId) {
        log.info("dynamic:{}", dynamic);
        Users user = usersMapper.getUserById(userId);

        Dynamic dy = Dynamic.builder().userId(userId).title(dynamic.getTitle()).content(dynamic.getContent()).imgUrl(
            dynamic.getImgUrl()).createTime(LocalDateTime.now()).userAvatar(user.getAvatarUrl()).nickname(
            user.getNickname()).build();
        dynamicMapper.insertDynamic(dy);
    }

    /**
     * 分页获取动态
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<DynamicVO> listDynamic(Integer pageNum, Integer pageSize) {
        PageBean<DynamicVO> page = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<DynamicVO> list = dynamicMapper.listDynamic();
        Page<DynamicVO> dynamic = (Page<DynamicVO>)list;
        page.setTotal(dynamic.getTotal());
        page.setItem(dynamic.getResult());

        return page;
    }

    @Override
    public List<DynamicVO> listDynamicByUserId(Long userId) {
        List<DynamicVO> dynamicVOS = dynamicMapper.listDynamicByUserId(userId);

        return dynamicVOS;
    }

    @Override
    public void deleteDynamicById(Long dynamicId) {
        dynamicMapper.deleteDynamicById(dynamicId);
    }

    @Override
    @Transactional
    public Boolean insertDynamicLike(Long dynamicId, Long userId) {
        Long id = dynamicMapper.queryDynamicLike(dynamicId, userId);
        if (id != null) {
            return true;
        }
        dynamicMapper.insertDynamicLike(dynamicId, userId, LocalDateTime.now());
        dynamicMapper.incrementDynamicLikeCount(dynamicId);
        return false;
    }

    @Override
    public void deleteDynamicLikeById(Long dynamicId, Long userId) {
        dynamicMapper.deleteDynamicLikeById(dynamicId, userId);
        dynamicMapper.decrementDynamicLikeCount(dynamicId);
    }

    @Override
    public void insertDynamicComment(DynamicCommentDTO dynamicComment,Long dynamicId, Long userId) {
        DynamicComment dynamicComment1 = DynamicComment.builder().dynamicId(dynamicId).userId(
            userId).content(dynamicComment.getContent()).createTime(LocalDateTime.now()).userAvatarUrl(
            dynamicComment.getUserAvatarUrl()).userNickname(dynamicComment.getUserNickname()).build();
        dynamicMapper.insertDynamicComment(dynamicComment1);

    }

    @Override
    public PageBean<DynamicCommentVO> listDynamicCommentById(Integer pageNum, Integer pageSize, Long dynamicId) {

        PageBean<DynamicCommentVO> page = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<DynamicCommentVO> dynamicComments = dynamicMapper.listDynamicCommentById(pageNum, pageSize, dynamicId);

        Page<DynamicCommentVO> dynamic = (Page<DynamicCommentVO>)dynamicComments;

        page.setTotal(dynamic.getTotal());
        page.setItem(dynamic.getResult());

        return page;
    }

    @Override
    public DynamicVO getDynamicById(Long dynamicId) {
        return dynamicMapper.getDynamicById(dynamicId);
    }

    @Override
    public Boolean checkUserLikeDynamic(Long dynamicId, Long userId) {
        Long id = dynamicMapper.checkUserLikeDynamic(dynamicId, userId);
        if (id != null)
            return true;
        return false;
    }

}
